<template>
  <view>
    <!-- 单选的配置 -->
    <pretty-times @change="getTime" :timeInterval="10" :isMultiple="false"
                  :disableTimeSlot="disableTimeSlot"></pretty-times>

    <!-- 多选时间的配置 -->
    <!-- <times @change="getTime" :timeInterval="1" :appointTime="appointTime"
    :isMultiple="true" :disableTimeSlot = "disableTimeSlot"></times> -->

    <view>
      <!-- 提示窗示例 -->
      <uni-popup ref="alertDialog" type="dialog">
        <uni-popup-dialog type="success" cancelText="取消" confirmText="预约" title="告知"
                          :content="'预约下单需要从钱包扣减' + price + '元，是否继续?'"
                          @confirm="appointmentDo" @close="dialogClose"></uni-popup-dialog>
      </uni-popup>
    </view>
  </view>
</template>

<script>
import {listRobotAppointmentByRobotId} from "@/api/qd/queue";
import {getRobot} from "@/api/qd/robot";
import store from "@/store";
import {appointment} from "@/api/qd/order";

export default {
  data() {
    return {
      robotId: 0,
      disableTimeSlot: [],
      timer: null,
      price: null,
      appointmentTime: null,
    }
  },
  onLoad(e) {
    this.robotId = e.id;
    this.getRobot();
    this.listRobotAppointment();
    // this.onSetTimeout();
  },
  onUnload() {
    // this.clearTimer();
  },
  methods: {
    getRobot() {
      getRobot(this.robotId).then((res) => {
        this.price = res.data.price;
      })
    },
    onSetTimeout() {
      this.timer = setInterval(() => {
        this.listRobotAppointment();
      }, 1000);
    },
    clearTimer() {
      if (this.timer) {
        clearTimeout(this.timer);
        this.timer = null;
      }
    },
    listRobotAppointment() {
      listRobotAppointmentByRobotId(this.robotId).then((res) => {
        this.disableTimeSlot = res.data;
      })
    },
    getTime(e) {
      this.appointmentTime = e;
      this.$refs.alertDialog.open()
    },
    appointmentDo() {
      let orderAppointmentReq = {
        userId: store.getters.userId,
        robotId: this.robotId,
        amount: this.price,
        type: 60,
        appointmentTime: this.appointmentTime,
      }
      appointment(orderAppointmentReq).then(res => {
        this.$modal.confirm('预约成功，是否查看我的预约？').then(() => {
          this.$tab.switchTab('/pages/appointment/myAppointment');
        })
      }).catch(err => {
        reject(err);
      })
    },
  }
}
</script>

<style lang="scss">
</style>

